<template>

<a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="100px">
    </a-col>

    <a-col flex="auto" >
      <div class="menu-demo">
      <a-menu mode="horizontal" :selected-keys="selectedKeys" @menu-item-click="MenuClick">
        <a-menu-item key="0" :style="{ padding: 0, marginRight: '38px' }" disabled>
          <div class="title-bar">
          <img class="logo" src="../assets/oj-logo.jpg">
          <div class="title">鱼OJ</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">{{ item.name }}</a-menu-item>
      </a-menu>
    </div>
    </a-col>

    <a-col flex="100px">
      <div>{{ store.state.user?.loginUser?.userName ?? "未登录" }}</div>
    </a-col>
  </a-row>
   
</template>
  
<style scoped>
  .menu-demo {
    box-sizing: border-box;
    width: 100%;
    padding: 20px;
    
  }
  .logo{
    height: 48px;
  }
  .title-bar{
    display: flex;
    align-items: center;
  }
  .title{
    font-size: 18px;
    color: #444;
    margin-left: 16px;
  }

</style>

<script setup lang="ts">

import { useRouter } from "vue-router";
import {routes} from "../router/routes"
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";

//默认主页
const selectedKeys=ref(["/"]);
const router=useRouter();
const store=useStore();
const loginUser=store.state.user.loginUser;

const visibleRoutes = computed(() => {
 return routes.filter((item,index)=>{
   if(item.meta?.hideInMenu){
      return false;
   }
   if(!checkAccess(store.state.user.loginUser,item.meta?.access as string)){
      return false;
   }
      return true;
  });
});

router.afterEach((to,)=>{
   selectedKeys.value=[to.path];
});

setTimeout(()=>{
    store.dispatch("user/getLoginUser",
    {
       userName:"lhy管理员",
       userRole:ACCESS_ENUM.ADMIN,
    });
},3000);

const MenuClick=(key:string)=>{
   router.push({
      path:key,
   });
};
</script>
  